package com.sxt.sse.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SseController {

    @RequestMapping(value = "/get_data",produces = "text/event-stream;charset=UTF-8")
    public String sse(String id)
    {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(id);
        return "行情为"+ Math.random();
    }
    
    @RequestMapping(value="/push",produces="text/event-stream;charset=UTF-8")
    public String push(HttpServletResponse res){
    
    //处理跨域请求
    res.setHeader("Access-Control-Allow-Origin","*");

    Date date=new Date();
    SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
    String nowDate=sdf.format(date);

    return "data: 我是一个data 现在时间是"+nowDate+" \nevent:like\n retry:5000\n\n";

    } 
}
